Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.UIManager = exports.layerType = undefined;
var $2ConstValue = require("ConstValue");
var i;
(function (e) {
  e[e.None = 0] = "None";
  e[e.View = 1] = "View";
  e[e.Controller = 2] = "Controller";
  e[e.StaticView = 3] = "StaticView";
  e[e.Mask = 4] = "Mask";
  e[e.Popup = 5] = "Popup";
  e[e.Tip_Popup = 6] = "Tip_Popup";
  e[e.Guide = 7] = "Guide";
})(i = exports.layerType || (exports.layerType = {}));
var exp_UIManager = /* */function () {
  function _ctor() {
    babel_classCallCheck(this, _ctor);
    this.uiList = [];
    this.uiRoot = null;
    this.zIndexList = [];
    this.layerNodeList = [];
    this.loadUiList = [];
    this.loading = null;
    this.uiRoot = cc.find("Canvas");
    for (var _e51 = 0; _e51 <= i.Guide; _e51++) {
      this.uiList[_e51] = new Map();
      this.zIndexList[_e51] = 0;
      var _t52 = this.uiRoot.getChildByName(i[_e51]);
      if (!_t52) {
        (_t52 = new cc.Node(i[_e51])).setParent(this.uiRoot);
        var _n49 = _t52.addComponent(cc.Widget);
        _n49.isAlignBottom = _n49.isAlignTop = _n49.isAlignRight = _n49.isAlignLeft = true;
        _n49.top = _n49.left = _n49.bottom = _n49.right = 0;
        _n49.updateAlignment();
      }
      _t52.zIndex = _e51;
      this.layerNodeList[_e51] = _t52;
    }
  }
  return babel_createClass(_ctor, [{
    key: "loadUI",
    value: function loadUI(e, t, n) {
      if (this.getUI(e)) {
        n && n();
      } else {
        cc.loader.loadRes($2ConstValue.ConstValue.PREFAB_UI_DIR + e.getUrl(), function (e, n, o) {
          t && t(e, n, o);
        }, function (e, t) {
          n && n(e, t);
        });
      }
    }
  }, {
    key: "openUI",
    value: function openUI(e, t, n) {
      var _this101 = this;
      var _len5 = arguments.length;
      var o = new Array(_len5 > 3 ? _len5 - 3 : 0);
      for (var _key5 = 3; _key5 < _len5; _key5++) {
        o[_key5 - 3] = arguments[_key5];
      }
      this.getUI(e) || this.loadUI(e, n, function (n, i) {
        if (n) {
          return void cc.log(n);
        }
        if (_this101.getUI(e)) {
          return void console.log("已经加载完成！", e.name);
        }
        var _s11 = _this101.getUI(e);
        var a = null;
        if (_s11) {
          a = _s11.node;
        } else {
          (_s11 = (a = cc.instantiate(i)).getComponent(e)).tag = e;
          _this101.pushUI(_s11);
        }
        a.parent = _this101.layerNodeList[e.getLayertype()] || _this101.uiRoot;
        a.setPosition(cc.v2(0, 0));
        t && t(_s11, o);
      });
    }
  }, {
    key: "pushUI",
    value: function pushUI(e) {
      console.log("pushUI", e.name);
      var t = e.tag.getLayertype();
      this.uiList[t].set(e.tag.getUrl(), e);
    }
  }, {
    key: "closeUI",
    value: function closeUI(e) {
      if (!e) {
        return;
      }
      console.log("closeUI", e.name);
      var t = this.uiList[e.getLayertype()];
      var n = t.get(e.getUrl());
      if (n) {
        console.log("destroy");
        n.node.destroy();
      }
      t.delete(e.getUrl());
    }
  }, {
    key: "showUI",
    value: function showUI(e, t, n) {
      var _this102 = this;
      e.getUrl();
      if (null != this.loading) {
        return void this.addLoadUiList(e, t);
      }
      this.loading = e;
      var o = this.getUI(e);
      if (o) {
        this.show(o, t);
      } else {
        this.openUI(e, function (e) {
          _this102.show(e, t);
        }, n);
      }
    }
  }, {
    key: "clearLoad",
    value: function clearLoad() {
      for (var _e52 = this.loadUiList.length - 1; _e52 >= 0; _e52--) {
        this.loadUiList[_e52].getAutomatic() && this.loadUiList.splice(_e52, 1);
      }
    }
  }, {
    key: "addLoadUiList",
    value: function addLoadUiList(e, t) {
      var n = e.getLayertype();
      e.getAutomatic() && (n == i.View || i.Popup);
      var o = {
        uiClass: e,
        callback: t
      };
      this.loadUiList.push(o);
    }
  }, {
    key: "lodeSuccess",
    value: function lodeSuccess() {
      this.loading = null;
      if (this.loadUiList.length > 0) {
        var _e53 = this.loadUiList.shift();
        this.showUI(_e53.uiClass, _e53.callback);
      }
    }
  }, {
    key: "loadFail",
    value: function loadFail() {
      this.loading = null;
    }
  }, {
    key: "show",
    value: function show(e, t) {
      if (0 != cc.isValid(e.node, true)) {
        e.node.active = true;
        if (0 == e.opened || null != this.loading && this.loading != e.tag) {
          e.tag.getAutomatic() && this.hideUI(e.tag);
        } else {
          switch (e.tag.getLayertype()) {
            case i.View:
          }
          e.node.zIndex = ++this.zIndexList[e.tag.getLayertype()];
          e.onShow();
        }
        t && t(e);
        this.lodeSuccess();
      } else {
        this.lodeSuccess();
      }
    }
  }, {
    key: "closeAlUi",
    value: function closeAlUi(e) {
      this.closeAll(this.uiList[e]);
    }
  }, {
    key: "closeAll",
    value: function closeAll(e, t) {
      var _this103 = this;
      e.forEach(function (e) {
        if (_this103.IsShow(e.tag) && e.tag.getAutomatic() && (null == t || e.tag.getUrl() !== t.tag.getUrl())) {
          console.log("ui:", e.name, "自动关闭：", e.tag.getAutomatic());
          e.close();
        }
      });
    }
  }, {
    key: "hideAll",
    value: function hideAll(e, t) {
      var _this104 = this;
      e.forEach(function (e) {
        _this104.IsShow(e.tag) && e.tag.getAutomatic() && (null == t || e.tag.getUrl() !== t.tag.getUrl()) && _this104.hideUI(e.tag);
      });
    }
  }, {
    key: "hideUI",
    value: function hideUI(e) {
      var t = this.getUI(e);
      t && (t.node.active = false);
    }
  }, {
    key: "getUI",
    value: function getUI(e) {
      if (null == e) {
        return null;
      }
      return this.uiList[e.getLayertype()].get(e.getUrl()) || null;
    }
  }, {
    key: "IsShow",
    value: function IsShow(e) {
      var t = this.getUI(e);
      return t && t.node.active;
    }
  }, {
    key: "getFace",
    value: function getFace() {
      var _iterator7 = babel_createForOfIteratorHelper(this.uiList[i.View].values());
      var _step7;
      try {
        for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
          var _e54 = _step7.value;
          if (this.IsShow(_e54.tag)) {
            return _e54;
          }
        }
      } catch (err) {
        _iterator7.e(err);
      } finally {
        _iterator7.f();
      }
    }
  }], [{
    key: "getInstance",
    value: function getInstance() {
      null == this.instance && (this.instance = new _ctor());
      return this.instance;
    }
  }]);
}();
exports.UIManager = exp_UIManager;
exp_UIManager.instance = null;